{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "美股-实时行情"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [],
   "source": [
    "import akshare as ak\n",
    "\n",
    "us_stock_current_df = ak.stock_us_spot()\n",
    "us_stock_current_df.tail(5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "名称与代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "import akshare as ak\n",
    "symbol=ak.stock_us_spot_em() "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>序号</th>\n",
       "      <th>名称</th>\n",
       "      <th>最新价</th>\n",
       "      <th>涨跌额</th>\n",
       "      <th>涨跌幅</th>\n",
       "      <th>开盘价</th>\n",
       "      <th>最高价</th>\n",
       "      <th>最低价</th>\n",
       "      <th>昨收价</th>\n",
       "      <th>总市值</th>\n",
       "      <th>市盈率</th>\n",
       "      <th>成交量</th>\n",
       "      <th>成交额</th>\n",
       "      <th>振幅</th>\n",
       "      <th>换手率</th>\n",
       "      <th>代码</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>11548</th>\n",
       "      <td>11549</td>\n",
       "      <td>Lemonade Inc Wt</td>\n",
       "      <td>0.02</td>\n",
       "      <td>-0.025</td>\n",
       "      <td>-54.57</td>\n",
       "      <td>0.027</td>\n",
       "      <td>0.027</td>\n",
       "      <td>0.02</td>\n",
       "      <td>0.045</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>113130.0</td>\n",
       "      <td>2581.0</td>\n",
       "      <td>15.59</td>\n",
       "      <td>NaN</td>\n",
       "      <td>107.LMND_WS</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          序号               名称   最新价    涨跌额    涨跌幅    开盘价    最高价   最低价    昨收价  \\\n",
       "11548  11549  Lemonade Inc Wt  0.02 -0.025 -54.57  0.027  0.027  0.02  0.045   \n",
       "\n",
       "       总市值  市盈率       成交量     成交额     振幅  换手率           代码  \n",
       "11548  NaN  NaN  113130.0  2581.0  15.59  NaN  107.LMND_WS  "
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "symbol.tail(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>名称</th>\n",
       "      <th>代码</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Volato Group Inc Wt</td>\n",
       "      <td>107.SOAR_WS</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Beneficient-A</td>\n",
       "      <td>105.BENF</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Safety Shot Inc Wt</td>\n",
       "      <td>105.SHOTW</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Yotta Acquisition Corp Wt</td>\n",
       "      <td>105.YOTAW</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Hall of Fame Resort &amp; Entertain</td>\n",
       "      <td>105.HOFVW</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11544</th>\n",
       "      <td>Nukkleus Inc Wt</td>\n",
       "      <td>105.NUKKW</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11545</th>\n",
       "      <td>LL Flooring Holdings Inc</td>\n",
       "      <td>106.LL</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11546</th>\n",
       "      <td>VerifyMe Inc Wt</td>\n",
       "      <td>105.VRMEW</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11547</th>\n",
       "      <td>Estrella Immunopharma Inc Wt</td>\n",
       "      <td>105.ESLAW</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11548</th>\n",
       "      <td>Lemonade Inc Wt</td>\n",
       "      <td>107.LMND_WS</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>11549 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                    名称           代码\n",
       "0                  Volato Group Inc Wt  107.SOAR_WS\n",
       "1                        Beneficient-A     105.BENF\n",
       "2                   Safety Shot Inc Wt    105.SHOTW\n",
       "3            Yotta Acquisition Corp Wt    105.YOTAW\n",
       "4      Hall of Fame Resort & Entertain    105.HOFVW\n",
       "...                                ...          ...\n",
       "11544                  Nukkleus Inc Wt    105.NUKKW\n",
       "11545         LL Flooring Holdings Inc       106.LL\n",
       "11546                  VerifyMe Inc Wt    105.VRMEW\n",
       "11547     Estrella Immunopharma Inc Wt    105.ESLAW\n",
       "11548                  Lemonade Inc Wt  107.LMND_WS\n",
       "\n",
       "[11549 rows x 2 columns]"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "symbol = symbol[['名称','代码']]\n",
    "symbol"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0          软件\n",
      "1         计算机\n",
      "2         半导体\n",
      "3         互联网\n",
      "4        媒体内容\n",
      "         ... \n",
      "14450      股权\n",
      "14454      股权\n",
      "14474      媒体\n",
      "14478      股权\n",
      "14495      电信\n",
      "Name: category, Length: 4530, dtype: object\n"
     ]
    }
   ],
   "source": [
    "df_cleaned = us_stock_current_df.dropna(subset=['category'])\n",
    "# 打印清理后的 DataFrame\n",
    "print(df_cleaned['category'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_cleaned.to_csv('美股.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "\n",
    "# 读取 CSV 文件并将其加载到 DataFrame 中\n",
    "df = pd.read_csv('美股.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "df=(df[(df['category'] == '软件') & \n",
    "         (df['price'] > 10) & \n",
    "         (df['volume'] > 100000) &\n",
    "         (df['market'] == 'NASDAQ')\n",
    "         ])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Unnamed: 0</th>\n",
       "      <th>name</th>\n",
       "      <th>cname</th>\n",
       "      <th>category</th>\n",
       "      <th>symbol</th>\n",
       "      <th>price</th>\n",
       "      <th>diff</th>\n",
       "      <th>chg</th>\n",
       "      <th>preclose</th>\n",
       "      <th>open</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>amplitude</th>\n",
       "      <th>volume</th>\n",
       "      <th>mktcap</th>\n",
       "      <th>pe</th>\n",
       "      <th>market</th>\n",
       "      <th>category_id</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2004</th>\n",
       "      <td>3190</td>\n",
       "      <td>Digi International, Inc.</td>\n",
       "      <td>美国迪进国际</td>\n",
       "      <td>软件</td>\n",
       "      <td>DGII</td>\n",
       "      <td>22.94</td>\n",
       "      <td>0.24</td>\n",
       "      <td>1.06</td>\n",
       "      <td>22.70</td>\n",
       "      <td>22.70</td>\n",
       "      <td>22.95</td>\n",
       "      <td>22.61</td>\n",
       "      <td>1.50%</td>\n",
       "      <td>125958</td>\n",
       "      <td>834433229</td>\n",
       "      <td>58.820516</td>\n",
       "      <td>NASDAQ</td>\n",
       "      <td>14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2034</th>\n",
       "      <td>3260</td>\n",
       "      <td>Simulations Plus, Inc.</td>\n",
       "      <td>Simulations Plus, Inc.</td>\n",
       "      <td>软件</td>\n",
       "      <td>SLP</td>\n",
       "      <td>38.89</td>\n",
       "      <td>-1.00</td>\n",
       "      <td>-2.51</td>\n",
       "      <td>39.89</td>\n",
       "      <td>40.01</td>\n",
       "      <td>40.06</td>\n",
       "      <td>38.58</td>\n",
       "      <td>3.71%</td>\n",
       "      <td>233301</td>\n",
       "      <td>778091196</td>\n",
       "      <td>81.020834</td>\n",
       "      <td>NASDAQ</td>\n",
       "      <td>14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2258</th>\n",
       "      <td>3771</td>\n",
       "      <td>Mitek Systems, Inc.</td>\n",
       "      <td>Mitek Systems, Inc.</td>\n",
       "      <td>软件</td>\n",
       "      <td>MITK</td>\n",
       "      <td>10.96</td>\n",
       "      <td>-0.40</td>\n",
       "      <td>-3.52</td>\n",
       "      <td>11.36</td>\n",
       "      <td>11.27</td>\n",
       "      <td>11.31</td>\n",
       "      <td>10.88</td>\n",
       "      <td>3.83%</td>\n",
       "      <td>644025</td>\n",
       "      <td>512825098</td>\n",
       "      <td>-68.500002</td>\n",
       "      <td>NASDAQ</td>\n",
       "      <td>14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3163</th>\n",
       "      <td>5976</td>\n",
       "      <td>Synchronoss Technologies, Inc.</td>\n",
       "      <td>Synchronoss Technologies, Inc.</td>\n",
       "      <td>软件</td>\n",
       "      <td>SNCR</td>\n",
       "      <td>10.22</td>\n",
       "      <td>0.82</td>\n",
       "      <td>8.72</td>\n",
       "      <td>9.40</td>\n",
       "      <td>9.39</td>\n",
       "      <td>10.30</td>\n",
       "      <td>9.11</td>\n",
       "      <td>12.69%</td>\n",
       "      <td>108168</td>\n",
       "      <td>110296001</td>\n",
       "      <td>-3.069069</td>\n",
       "      <td>NASDAQ</td>\n",
       "      <td>14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3525</th>\n",
       "      <td>7219</td>\n",
       "      <td>Global X Autonomous &amp; Electric Vehicles ETF</td>\n",
       "      <td>Global X Autonomous &amp; Electric Vehicles ETF</td>\n",
       "      <td>软件</td>\n",
       "      <td>DRIV</td>\n",
       "      <td>24.26</td>\n",
       "      <td>0.25</td>\n",
       "      <td>1.04</td>\n",
       "      <td>24.01</td>\n",
       "      <td>24.06</td>\n",
       "      <td>24.29</td>\n",
       "      <td>24.04</td>\n",
       "      <td>1.05%</td>\n",
       "      <td>113133</td>\n",
       "      <td>46094000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NASDAQ</td>\n",
       "      <td>14</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      Unnamed: 0                                         name  \\\n",
       "2004        3190                     Digi International, Inc.   \n",
       "2034        3260                       Simulations Plus, Inc.   \n",
       "2258        3771                          Mitek Systems, Inc.   \n",
       "3163        5976               Synchronoss Technologies, Inc.   \n",
       "3525        7219  Global X Autonomous & Electric Vehicles ETF   \n",
       "\n",
       "                                            cname category symbol  price  \\\n",
       "2004                                       美国迪进国际       软件   DGII  22.94   \n",
       "2034                       Simulations Plus, Inc.       软件    SLP  38.89   \n",
       "2258                          Mitek Systems, Inc.       软件   MITK  10.96   \n",
       "3163               Synchronoss Technologies, Inc.       软件   SNCR  10.22   \n",
       "3525  Global X Autonomous & Electric Vehicles ETF       软件   DRIV  24.26   \n",
       "\n",
       "      diff   chg  preclose   open   high    low amplitude  volume     mktcap  \\\n",
       "2004  0.24  1.06     22.70  22.70  22.95  22.61     1.50%  125958  834433229   \n",
       "2034 -1.00 -2.51     39.89  40.01  40.06  38.58     3.71%  233301  778091196   \n",
       "2258 -0.40 -3.52     11.36  11.27  11.31  10.88     3.83%  644025  512825098   \n",
       "3163  0.82  8.72      9.40   9.39  10.30   9.11    12.69%  108168  110296001   \n",
       "3525  0.25  1.04     24.01  24.06  24.29  24.04     1.05%  113133   46094000   \n",
       "\n",
       "             pe  market  category_id  \n",
       "2004  58.820516  NASDAQ           14  \n",
       "2034  81.020834  NASDAQ           14  \n",
       "2258 -68.500002  NASDAQ           14  \n",
       "3163  -3.069069  NASDAQ           14  \n",
       "3525        NaN  NASDAQ           14  "
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.tail(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "27    DGII\n",
       "28     SLP\n",
       "29    MITK\n",
       "30    SNCR\n",
       "31    DRIV\n",
       "Name: symbol, dtype: object"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.reset_index(drop=True)\n",
    "df['symbol'].reset_index(drop=True).tail(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "              日期      开盘      收盘      最高      最低     成交量        成交额    振幅  \\\n",
      "0     2020-01-02  14.364  14.488  14.488  14.306   16735   251507.0  1.29   \n",
      "1     2020-01-03  14.364  14.277  14.364  14.210   29954   447287.0  1.06   \n",
      "2     2020-01-06  14.182  14.277  14.277  14.076   12128   179720.0  1.41   \n",
      "3     2020-01-07  14.402  14.373  14.421  14.306   19069   286114.0  0.81   \n",
      "4     2020-01-08  14.478  14.478  14.507  14.344   27592   416676.0  1.13   \n",
      "...          ...     ...     ...     ...     ...     ...        ...   ...   \n",
      "1032  2024-02-08  23.269  23.388  23.467  23.160  128488  3019258.0  1.32   \n",
      "1033  2024-02-09  23.467  23.666  23.690  23.398  115375  2738978.0  1.25   \n",
      "1034  2024-02-12  23.606  23.924  24.142  23.606  119806  2890123.0  2.26   \n",
      "1035  2024-02-13  23.418  23.140  23.423  22.971  166119  3884890.0  1.89   \n",
      "1036  2024-02-14  23.329  23.537  23.567  23.229  119375  2819124.0  1.46   \n",
      "\n",
      "       涨跌幅    涨跌额  换手率  \n",
      "0     2.30  0.326  0.0  \n",
      "1    -1.46 -0.211  0.0  \n",
      "2     0.00  0.000  0.0  \n",
      "3     0.67  0.096  0.0  \n",
      "4     0.73  0.105  0.0  \n",
      "...    ...    ...  ...  \n",
      "1032  0.77  0.179  0.0  \n",
      "1033  1.19  0.278  0.0  \n",
      "1034  1.09  0.258  0.0  \n",
      "1035 -3.28 -0.784  0.0  \n",
      "1036  1.72  0.397  0.0  \n",
      "\n",
      "[1037 rows x 11 columns]\n"
     ]
    }
   ],
   "source": [
    "import akshare as ak\n",
    "stock_us_hist_df = ak.stock_us_hist(symbol='105.DRIV', period=\"daily\", start_date=\"20200101\", end_date=\"20240214\", adjust=\"qfq\")\n",
    "print(stock_us_hist_df)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
